package com.crawler.proxy

import akka.actor.{Actor, ActorLogging}
import com.crawler.utils.HttpClientUtils
import com.alibaba.fastjson.JSON

import scala.concurrent.duration._

/**
  * Created by tongtao.zhu on 2017/2/6.
  */
class ProxyUtils extends Actor with ActorLogging{
  implicit val executionContext = context.system.dispatcher
  createProxy()

  override def receive = {
    case _ => createProxy()
  }

  //每隔60s更新一次Proxy
  context.system.scheduler.schedule(60 seconds, 60 seconds, self, "get")

  def createProxy(): Unit ={
    ProxyUtils.proxyList = List[CrawlProxy]()
    val list: String = HttpClientUtils.getInstance.sendHttpGet("http://poi-crawler-export-service01.gq:4080/invoke.json?url=http://service.dianping.com/poiService/HttpProxyExportService_1.0.0&method=queryCrawlerProxyInfo&parameterTypes=java.lang.String&parameters=5dfe6b30fac7788c&parameterTypes=long&parameters=60&parameterTypes=int&parameters=100&parameterTypes=int&parameters=200")

    var hostIp: String = null
    var hostPort: String = null
    var auth: String = null

    val array = JSON.parseArray(list)
    if (array.size() == 0){
      log.info("error proxy")
      return
    }
    for (i <- 0 to array.size()-1){
      val item: String = array.getString(i)
      val jsonObject = JSON.parseObject(item)
      hostIp = jsonObject.getString("hostIp")
      hostPort = jsonObject.getString("hostPort")
      val extInfo: String = jsonObject.getString("extInfo")
      auth = JSON.parseObject(extInfo).getString("a")
      ProxyUtils.proxyList = ProxyUtils.proxyList :+ CrawlProxy(hostIp, hostPort.toInt, auth.split(":")(0), auth.split(":")(1), 0, "")
    }
    log.info("开始更新Proxy,总数为%d".format(ProxyUtils.proxyList.size))
  }
}

object ProxyUtils{
  var proxyList: List[CrawlProxy] = List[CrawlProxy]()
}

case class CrawlProxy(ip: String, port: Int, user: String, password: String, errorCount: Int, addTime: String)